package com.selebash.runtime.resource;

import com.selebash.exception.SelebashException;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

/**
 * @author sad
 */
public class FileResourceManager extends ResourceManager {

    @Override
    public boolean isExist(String path) {
        return new File(path).exists();
    }

    @Override
    public String getContent(String path) {
        try {
            return FileUtils.readFileToString(new File(path), "UTF8");
        } catch (IOException ex) {
            throw new SelebashException("Cannot read file " + path);
        }
    }

    public String combinePaths(String path, String path2) {
        if (path == null) {
            return path2 == null ? "" : path2;
        }
        path = path.replace('\\', '/');
        path2 = path2.replace('\\', '/');
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        if (path2.startsWith("/")) {
            path2 = path2.substring(1);
        }

        return path + "/" + path2;
    }
    
    @Override
    public String combinePath(String path1, String path2) {
        return combinePaths(path1, path2);
    }
}
